using System;
namespace Microsoft.Phone.Controls.Maps
{
	public struct QuadKey
	{
		public int ZoomLevel
		{
			get;
			set;
		}
		public int X
		{
			get;
			set;
		}
		public int Y
		{
			get;
			set;
		}
		public string Key
		{
			get
			{
				return QuadKey.QuadPixelToQuadKey(this.X, this.Y, this.ZoomLevel);
			}
		}
		public QuadKey(int x, int y, int zoomLevel)
		{
			this = default(QuadKey);
			this.ZoomLevel = zoomLevel;
			this.X = x;
			this.Y = y;
		}
		public QuadKey(string quadKey)
		{
			this = default(QuadKey);
			int x;
			int y;
			int zoomLevel;
			QuadKey.QuadKeyToQuadPixel(quadKey, out x, out y, out zoomLevel);
			this.ZoomLevel = zoomLevel;
			this.X = x;
			this.Y = y;
		}
		private static string QuadPixelToQuadKey(int x, int y, int zoomLevel)
		{
			int num = (int)Math.Pow(2.0, (double)zoomLevel);
			string text = "";
			if (y >= 0)
			{
				if (y < num)
				{
					while (x < 0)
					{
						x += num;
					}
					while (x > num)
					{
						x -= num;
					}
					for (int i = 1; i <= zoomLevel; i++)
					{
						switch (2 * (y % 2) + x % 2)
						{
						case 0:
							text = "0" + text;
							break;
						case 1:
							text = "1" + text;
							break;
						case 2:
							text = "2" + text;
							break;
						case 3:
							text = "3" + text;
							break;
						}
						x /= 2;
						y /= 2;
					}
					return text;
				}
			}
			return null;
		}
		private static void QuadKeyToQuadPixel(string quadKey, out int x, out int y, out int zoomLevel)
		{
			x = 0;
			y = 0;
			zoomLevel = 0;
			if (!string.IsNullOrEmpty(quadKey))
			{
				zoomLevel = quadKey.get_Length();
				for (int i = 0; i < quadKey.get_Length(); i++)
				{
					switch (quadKey.get_Chars(i))
					{
					case '0':
						x *= 2;
						y *= 2;
						break;
					case '1':
						x = x * 2 + 1;
						y *= 2;
						break;
					case '2':
						x *= 2;
						y = y * 2 + 1;
						break;
					case '3':
						x = x * 2 + 1;
						y = y * 2 + 1;
						break;
					}
				}
			}
		}
		public static bool operator ==(QuadKey tile1, QuadKey tile2)
		{
			return tile1.X == tile2.X && tile1.Y == tile2.Y && tile1.ZoomLevel == tile2.ZoomLevel;
		}
		public static bool operator !=(QuadKey tile1, QuadKey tile2)
		{
			return !(tile1 == tile2);
		}
		public override bool Equals(object obj)
		{
			if (obj != null && obj is QuadKey)
			{
				QuadKey tile = (QuadKey)obj;
				return this == tile;
			}
			return false;
		}
		public override int GetHashCode()
		{
			return this.X.GetHashCode() ^ this.Y.GetHashCode() ^ this.ZoomLevel.GetHashCode();
		}
	}
}
